<extend name="Layout:layout_edit" />
<block name="body_content_block">
    <form action="{:AddonU('IncomingCall/rule_save')}" method="post">
        <table>
            <if condition="$isSuperAdmin">
                <th>租户ID：</th><td><select name="siteID" title="请选择租户" required data-url="{:AddonU('Admin/get_admin_json')}" data-value="{$data.siteID}"></select>
                <else/>
                <tr style="display: none;"><input type="hidden" name="siteID" value="{$loginData.siteID}"/></td></tr>
            </if>
            <tr>
                <th>导航名称：</th><td>
                    <input type="text" name="name" placeholder="请输入导航名称" value="{$data.name}" title="请填写导航名称!" required/>
                </td></tr>
            <tr>
                <th>中继名称：</th><td>
                    <select name="trunkName" title="请选择中继名称" required data-url="{:AddonU('Trunk/get_json')}" data-value="{$data.trunkName}"></select>
                    <input type="hidden" name="regMode" value="{$data.regMode}"/>
                </td></tr>
            <tr>
                <th>被叫号码</th><td>(多个之间以英文逗号[,]隔开,如填写*则不写通话详单)
            <textarea name="calledNo" cols="60" rows="3" placeholder="请填写被叫号码">{$data.calledNo}</textarea>
            </td></tr>
            <tr>
                <th>主叫号码</th><td>
                    <input type="text" name="callerNo" title="请填写主叫号码" value="{$data.callerNo}" placeholder="请填写主叫号码"/>
                </td></tr>
            <tr>
                <th>呼通后操作：</th><td><!--顺序不能变,否则配置文件会乱! 2014.6.28 by sutroon-->
                    <select name='outOper' title="请选择呼通后操作" required>
                        <option>请选择操作</option>
                        <option value="0">直接转队列</option>
                        <option value="1">按码转队列</option>
                        <option value="6">放音转队列</option>
                        <option value="2">按码转菜单</option>
                        <option value="3">放音后挂机</option>
                        <option value="4">留言后挂机</option>
                        <option value="5">按码后挂机</option>
                    </select><span id="dynContent"></span>
                </td></tr>
            <tr>
                <th>队列号：</th><td>
                    <select name="outQueue" data-url="{:AddonU('Queue/get_queue_json')}" data-value="{$data.outQueue}"></select>
                </td></tr>
            <tr>
                <th>按码设置：</th><td>
                    <input type="text" readonly="readonly" size="20" name="outParam" value="{$data.outParam}"/>
                    <input type="button" class="button" onclick="create_outOperContent(this)" value="选择">
                    <a href="#" onclick="$(this).prev().prev().val('')">[x]</a>
                </td></tr>
            <tr>
                <th>是否评价：</th><td>
                    <input type="checkbox" name="outEcEnable" value="1"/>
                    <span style="display:none;">评价语音：<select name="outEcMusic" title="请选择评价语音" data-url="{:AddonU('Music/get_vocfile_json')}" data-value="{$data.outEcMusic}"></select> <!--评价按键：<input type="text" class="easyui-textbox" size="4" name="outEcKey" value="{$data.outEcKey}" />--></span>
                </td></tr>
            <tr>
                <th>放音文件：</th><td>
                    <select name="music" title="请选择欢迎语音" data-url="{:AddonU('Music/get_vocfile_json')}" data-value="{$data.music}"></select>
                </td></tr>
            <tr>
                <th>最大放音次数：</th><td>
                    <input type="text" name="musicLimit" placeholder="请填写数值" title="请填写最大放音次数" value="{$data.musicLimit|default='1'}" size="3"/>
                </td></tr>
            <tr>
                <th>录音标识：</th><td>
                    <select name="recState" title="请选择录音标识" required>
                        <option value="1">录音</option>
                        <option value="0">不录音</option>
                    </select>
                </td></tr>
            <tr>
                <th>播放工号：</th><td>
                    <select name="sayExtension" title="请选择播放工号" required>
                        <option value="1">播放</option>
                        <option value="0">不播放</option>
                    </select>
                </td></tr>
            <tr>
                <th>日期类型：</th><td>
                    <select name="dateType" title="请选择日期类型" required>
                        <option value="1">工作日</option>
                        <option value="0">非工作日</option>
                        <option value="2">所有</option>
                    </select>
                </td></tr>
            <tr>
                <th>开始时间：</th><td>
                    <input type="text" class="easyui-datebox" name="beginTime" class="timepicker" value="{$data.beginTime}"/>
                </td></tr>
            <tr>
                <th>结束时间：</th><td>
                    <input type="text" class="easyui-datebox" name="endTime" class="timepicker" value="{$data.endTime}"/>
                </td></tr>
        </table>
        <input type='hidden' value='{$data.id}' name='id'/>
    </form>
    <input type="hidden" id="musicmenu_url" value="{:AddonU('MusicMenu/get_json')}" />
</block>
<block name="body_after_block">
    <script>
        item_form.resize_owner_dialog({width: 540});
        if ('{$data.id}') {
            $('input[name="name"]').attr('readonly', true);
        }
        var strQueueOpts = '';
        var strVocMenuOpts = '';
        $('select[name="speedMode"] option[value="{$data.speedMode}"]').prop('selected', 'selected');
        $('select[name="outOper"] option[value="{$data.outOper}"]').prop('selected', 'selected');
        $('select[name="dateType"] option[value="{$data.dateType}"]').prop('selected', 'selected');
        $('select[name="recState"] option[value="{$data.recState}"]').prop('selected', 'selected');
        $('select[name="sayExtension"] option[value="{$data.sayExtension}"]').prop('selected', 'selected');

        if ('{$data.outEcEnable}' == '1') {
            $('input[name="outEcEnable"]').prop('checked', true).next().show().parent().parent().show();
        }
        $('input[name="outEcEnable"]').click(function () {
            $(this).next().toggle($(this).prop('checked'));
        })

        ajax_load_options({selector: 'select[name="siteID"]', def_option: '选择租户', func: afterSiteID});
        afterSiteID();
        function afterSiteID() {
            var val = $('[name="siteID"]').val();
            ajax_load_options({selector: 'select[name="trunkName"]', queryParams: {siteID: val}, def_option: '请选择中继名称'});
            ajax_load_options({selector: 'select[name="outQueue"]', queryParams: {siteID: val}, def_option: '选择队列号码', func: afterQueue});
            ajax_load_options({url: $('#musicmenu_url').val(), queryParams: {siteID: val}, def_option: '选择语音菜单', func: afterVocMenu});
            ajax_load_options({selector: 'select[name="music"]', queryParams: {type: "sounds", siteID: val}});
            ajax_load_options({selector: 'select[name="outEcMusic"]', queryParams: {type: "sounds", siteID: val}});
        }
        $('select[name="siteID"]').change(function () {
            afterSiteID();
        })

        set_dateType('{$data.dateType}');
        $('select[name="dateType"]').change(function () {
            set_dateType($(this).val());
        })
        function set_dateType(val) {
            var $li = $('select[name="dateType"]').parent().parent();
            if (val == 2) {
                $li.next().hide();
                $li.next().next().hide();
            } else {
                $li.next().show();
                $li.next().next().show();
            }
        }
        function afterQueue(opts) {
            strQueueOpts = opts;
            set_outOper('{$data.outOper}');
            $('input[name="outParam"]').val('{$data.outParam}');
        }
        function afterVocMenu(opts) {
            strVocMenuOpts = opts;
            set_outOper('{$data.outOper}');
            $('input[name="outParam"]').val('{$data.outParam}');
        }
        // 中继号码
        $('select[name="trunkName"]').change(function () {
            var txt = $(this).find('option:selected').text();
            set_trunkName((txt.indexOf('(ip|') > -1) ? 'ip' : 'register');
        });
        function set_trunkName(regMode) {
            if (regMode == 'ip') {
                $('input[name="regMode"]').val('ip');
                $('input[name="calledNo"]').val('');
            } else {
                $('input[name="regMode"]').val('register');
                var txt = $('select[name="trunkName"] option:selected').text();
                txt = txt.substring(txt.lastIndexOf('|') + 1);
                txt = txt.substring(0, txt.length - 1);
                $('input[name="calledNo"]').val(txt);
            }
        }
        // 呼通后操作
        /*
         $('select[name="outOper"]').change(function () {
         var val = $(this).val();
         set_outOper(val);
         });
         */
        $('select[name="outOper"]').change(function () {
                var val = $(this).val();
                set_outOper(val);
            });
        function set_outOper(val) {
            $('select[name="music"]').parent().parent().show();
            $('input[name="musicLimit"]').parent().parent().show();
            if (val == '') {
                return;
            }
            $('select[name="sayExtension"]').parent().parent().toggle((',0,1,6,'.indexOf(',' + val + ',') > -1));
            $('input[name="musicLimit"]').parent().parent().toggle((',1,2,3,4,5,'.indexOf(',' + val + ',') > -1));
            $('select[name="music"]').parent().parent().toggle((',1,2,3,4,5,6,'.indexOf(',' + val + ',') > -1));
            // $('select[name="music"]').parent().parent().toggle((',0,7,'.indexOf(',' + val + ',') > -1));
            $('input[name="outEcEnable"]').parent().parent().toggle((',0,1,6,'.indexOf(',' + val + ',') > -1));
            $('input[name="outParam"]').val('').parent().parent().toggle((',1,2,'.indexOf(',' + val + ',') > -1));
            $('select[name="outQueue"]').parent().parent().toggle((',0,6,'.indexOf(',' + val + ',') > -1));
        }
        function create_outOperContent(osrc) {
            var options = '';
            var val = $('select[name="outOper"]').val();
            var type = '';
            if ((',1,6,8,9,'.indexOf(',' + val + ',') > -1)) {
                type = 'queue';
            }
            if ((',2,'.indexOf(',' + val + ',') > -1)) {
                type = 'menu';
            }
            if (type == '') {
                return;
            }
            switch (type) {
                case 'queue':
                    options = strQueueOpts;
                    break;
                case 'menu':
                    options = strVocMenuOpts;
                    break;
            }
            var params = $(osrc).prev().val();
            var str = '<div style="width:500px; height:300px; display:table;" title="设置" id="dlg-outoper"><table>';
            var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'x'];
            for (var i = 0; i < data.length; i++) {
                str += '<tr style="float:left; margin:5px 10px 0px 0px;">' + data[i] + '键<select class="outOperContent-picker" data-id="' + data[i] + '">' + select_optParam_dialog(data[i], params, options) + '</select>' + ((i > 0 && (i + 1) % 3 < 1) ? '<br />' : '') + '</td></tr>';
            }
            str += '</table></div>';
            $(str).dialog({
                width: 390,
                height: 200,
                buttons: [
                    {text: "关闭", handler: function () {
                            var $dlg = $('#dlg-outoper').dialog("close").parent();
                            $dlg.next().remove();
                            $dlg.remove();
                        }}
                ]
            });
            $('#dlg-outoper').find('select').change(function () {
                var val = $(this).val();
                var id = $(this).attr('data-id');
                var $input = $(osrc).prev();
                var str = $input.val();
                $input.val(replace_value(str, id, isNaN(val) ? 'REMOVE' : val));
            });
        }
        /**
         * 正则选择符合指定值的Option标签
         * @param {string} str options字符串
         * @param {mixed} val 值
         * @returns {string} 返回包含selected="selected"的options字符串
         * @since 1.0 2014-6-27 by sutroon
         */
        function set_option_selected(str, val) {
            if (!val || !str)
                return str;
            var pattern = eval('/=["|\']?' + val + '["|\']?/g');
            str = str.replace(pattern, '="' + val + '" selected="selected"');
            return str;
        }
        function select_optParam_dialog(id, params, opts) {
            if (!params)
                return opts;
            if (params.indexOf(':') < 0) {
                return set_option_selected(opts, params);
            }
            var arr1 = params.split(';');
            var arr2;
            for (var i = 0; i < arr1.length; i++) {
                arr2 = arr1[i].split(':');
                if (arr2[0] == id) {
                    opts = set_option_selected(opts, arr2[1]);
                    break;
                }
            }
            return opts;
        }

        /**
         * 替换键值
         * @param {string} str
         * @param {mixed} id
         * @param {mixed} val
         * @returns {string}
         * @since 1.0 2014-6-26 by sutroon
         */
        function replace_value(str, id, val) {
            if (str.length > 0) {
                str = ';' + str;
                if (str.indexOf(';' + id + ':') > -1) {
                    var pattern = eval('/;' + id + ':\\d+[^;]?/g'); // 坑X!!!by sutroon
                    if (val == 'REMOVE') {
                        str = str.replace(pattern, '');
                    } else {
                        str = str.replace(pattern, ';' + id + ':' + val);
                    }
                } else {
                    str += ';' + id + ':' + val;
                }
                str = str.substring(1);
            } else {
                str = id + ':' + val;
            }
            return str;
        }
    </script>
</block>